/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.server;

import com.chasekernan.hxnova.utils.Utils;
import com.chasekernan.hxnova.core.dataholders.Global;

/**
    Stores all the host data for the entire game.
    It's built from [HostTurnFile]s.
**/
class HostFile {
    
    public var name : String;
    
    private var currentTurn : HostTurnFile;
    
    public function new(name : String) {
        this.name = name;
    }
    
    public function addTurn(htf : HostTurnFile) {
        if (htf == null) throw "Cannot add null host turn file.";
        
        currentTurn = htf;
    }
    
    /**
        Returns the turn based on the _game_ year.
        Will throw an error if this year is out of bounds.
    **/
    public function getTurn(year : Int) : HostTurnFile {
        try {
            var hostFileString = IO.uncompress(
                    IO.readFromFile(name + "/backup/" +
                    name + year + "." + Server.HOST_FILE_EXTENSION));
            var hostFileXml = Xml.parse(hostFileString).firstChild();
            return HostFile.readFromXml(hostFileXml).currentTurn;
        } catch (e : Dynamic) {
            throw "No host file present for given year.";
        }
    }
    
    /**
        Returns the last turn saved.
        Will throw an error if there are no turns saved.
    **/
    public function getLatestTurn() : HostTurnFile {
        return currentTurn;
    }
    
    public static var XML_TITLE = "host_file";
    
    /**
        Writes a host file to xml.
        Sample xml
            TODO: This might be a bit too big to put in a doc.
    **/
    public static function writeToXml(hf : HostFile) : Xml {
        var xml = Xml.parse('<' + XML_TITLE + '></' + XML_TITLE + '>')
                .firstChild();
        
        xml.addChild(HostTurnFile.writeToXml(hf.currentTurn));
        
        xml.set('name', hf.name);
        
        return xml;
    }
    
    public static function readFromXml(xml : Xml) : HostFile {
        try {            
            var hf = new HostFile(xml.get('name'));
            hf.currentTurn = HostTurnFile.readFromXml(
                    Utils.getXmlElement(xml, HostTurnFile.XML_TITLE));
			
            return hf;
        } catch (e : Dynamic) {
            throw "Error while reading host file: " + e;
        }
    }
    
    public static function loadHostFile(name : String) : HostFile {
        try {
            //loads from gameName/gameName.hxnova-host
            var hostFileString = IO.uncompress(IO.readFromFile(name + "/" +
                    name + "." + Server.HOST_FILE_EXTENSION));
            var hostFileXml = Xml.parse(hostFileString).firstChild();
            return HostFile.readFromXml(hostFileXml);
        } catch (e : Dynamic) {
            throw "Either the host file doesn't exist, or it is unreadable. " +
                  "Cannot continue... \n" + e;
        }
    }
    
    public static function saveHostFile(hf : HostFile) {
        IO.writeToFile(IO.compress(HostFile.writeToXml(hf).toString()), 
                hf.name + "/" + hf.name + "." + Server.HOST_FILE_EXTENSION);
    }
}